//
//  ZJNetWorkingHelper.m
//  CP-Fitness
//
//  Created by 张敬 on 2017/11/17.
//  Copyright © 2017年 Jing Zhang. All rights reserved.
//

#import "TJNetWorkingHelper.h"
//#import "NSString+Signature.h"
#import "NSString+TimeConvert.h"

#import "TokenCacheManager.h"
#import "UIDevice+KeychainIDFV.h"
#import "SystemUtil.h"

@implementation TJNetWorkingHelper

+ (AFHTTPSessionManager *)sharedSessionManager
{
    static AFHTTPSessionManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.requestSerializer.timeoutInterval = 30.0;
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        NSString *memberToken = [TokenCacheManager sharedInstance].memberToken;
        NSString *memberId = [NSString stringWithFormat:@"%@",[UserInfoDataProvider sharedInstance].userInfo.memberId];

        [manager.requestSerializer setValue:memberToken forHTTPHeaderField:@"token"];
        [manager.requestSerializer setValue:memberId forHTTPHeaderField:@"memberId"];
        [manager.requestSerializer setValue:[SystemUtil getUniqueId] forHTTPHeaderField:@"deviceNum"];
        [manager.requestSerializer setValue:@"application/json;" forHTTPHeaderField:@"Content-Type"];

        [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
                                                               options:NSJSONWritingPrettyPrinted
                                                                 error:error];

            NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
            return jsonString;
        }];

        
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain",nil];
        
    });
    return manager;
}

+ (void)postJsonWithUrl:(NSString *)urlStr
              parameter:(NSDictionary *)parameters
                success:(void (^)(NSDictionary*responseObject))success
                   fail:(void(^)(NSError *error))fail
{
    AFHTTPSessionManager *manager = [TJNetWorkingHelper sharedSessionManager];
    
    [manager POST:urlStr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject) {
            NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
            success(resultDic);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error) {
            fail(error);
        }
    }];
}

+ (void)getJsonWithUrl:(NSString *)urlStr
             parameter:(NSDictionary *)parameters
               success:(void (^)(NSDictionary*responseObject))success
                  fail:(void(^)(NSError *error))fail
{
    AFHTTPSessionManager *manager = [TJNetWorkingHelper sharedSessionManager];
    
    [manager GET:urlStr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject) {
            //id dataSet = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
            NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
            success(resultDic);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error) {
            fail(error);
        }
    }];
}


@end
